package org.stone926.airschool.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stone926.airschool.bean.Authority;
import org.stone926.airschool.bean.Student;
import org.stone926.airschool.bean.Teacher;
import org.stone926.airschool.bean.info.AbstractInfo;
import org.stone926.airschool.constant.LoginStatus;
import org.stone926.airschool.service.StudentService;
import org.stone926.airschool.service.TeacherService;
import org.stone926.airschool.utils.function.QuadraFunction;

@Controller
@CrossOrigin(value = "*", maxAge = 3600)
public class RegisterController {
	@Autowired
	private StudentService studentService;

	@Autowired
	private TeacherService teacherService;

	private Map<String, QuadraFunction<String, String, String, String, AbstractInfo>> registerActionMap = new HashMap<>();

	public RegisterController() {
		registerActionMap.put("student", (username, password, gender, phonenumber) -> {

			AbstractInfo info = studentService.studentRegister(new Student() {
				{
					setsName(username);
					setStudentPassword(password);
					setStudentAuthority(new Authority() {
						{
							setAid(2);
							setaType("user");
						}
					});
					setStudentGender(gender);
					setLoginStatus(LoginStatus.NOT_LOGGED_IN);
					setStudentPhonenumber(phonenumber);
				}
			});

			return info;
		});

		registerActionMap.put("teacher", (username, password, gender, phonenumber) -> {
			AbstractInfo info = teacherService.teacherRegister(new Teacher() {
				{
					setTeacherGender(gender);
					setTeacherPassword(password);
					setTeacherPhonenumber(phonenumber);
					settName(username);
				}
			});

			return info;
		});
	}

	@RequestMapping(value = "/register", method = RequestMethod.GET)
	public String register() {
		return "register";
	}

	@ResponseBody
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public AbstractInfo register(@RequestBody RegisterAjaxMessage req) {
		String identity = req.getIdentity();
		String username = req.getUsername();
		String password = req.getPassword();
		String gender = req.getGender();
		String phonenumber = req.getPhonenumber();
		AbstractInfo info = registerActionMap.get(identity).apply(username, password, gender, phonenumber);
		return info;
	}

	static class RegisterAjaxMessage {
		private String identity;
		private String username;
		private String password;
		private String gender;
		private String phonenumber;

		public String getIdentity() {
			return identity;
		}

		public void setIdentity(String identity) {
			this.identity = identity;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getGender() {
			return gender;
		}

		public void setGender(String gender) {
			this.gender = gender;
		}

		public String getPhonenumber() {
			return phonenumber;
		}

		public void setPhonenumber(String phonenumber) {
			this.phonenumber = phonenumber;
		}

		@Override
		public String toString() {
			return "RegisterAjaxMessage [identity=" + identity + ", username=" + username + ", password=" + password
					+ ", gender=" + gender + ", phonenumber=" + phonenumber + "]";
		}

	}
}
